﻿var map, searchManager, directionsManager;

function GetMap() {
    var mapOptions =
    {
        credentials: "YOUR_BING_MAPS_KEY",
        zoom: 2
    };

    map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOptions);
}

/******* Geocode Logic *****/

function GeocodeModule() {
    ClearMap();

    if (searchManager) {
        var request = { 
            where: document.getElementById('searchTbx').value,
            count:1,
            callback: geocodeCallback, 
            errorCallback: geocodeError
        };

        searchManager.geocode(request);
    } else {
        //Load the Search module and create a search manager.
        Microsoft.Maps.loadModule('Microsoft.Maps.Search', {
            callback: function () {
                //Create the search manager
                searchManager = new Microsoft.Maps.Search.SearchManager(map);

                //Perfrom search logic
                Geocode();
            }
        });
    }
}

function geocodeCallback(response, userData) {
    if (response &&
        response.results &&
        response.results.length > 0) {
        var r = response.results[0];
        var l = new Microsoft.Maps.Location(r.location.latitude, r.location.longitude);

        //Display result on map        
        var p = new Microsoft.Maps.Pushpin(l);
        map.entities.push(p);

        //Zoom to result
        map.setView({ center: l, zoom : 15 });
    } else {
        ShowMessage("Geocode Response", "Not results found.");
    }
}

function geocodeError(request) {
    ShowMessage("Geocode Error", "Unable to Geocode request.");
}

/******* Routing Logic *****/

function GetRoute() {
    ClearMap();

    if (directionsManager) {
        // Set Route Mode to driving 
        directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });

        // Create start and end waypoints
        var startWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: document.getElementById('fromTbx').value });
        var endWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: document.getElementById('toTbx').value });

        directionsManager.addWaypoint(startWaypoint);
        directionsManager.addWaypoint(endWaypoint);

        // Set the id of the div to use to display the directions
        directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('itineraryDiv') });

        // Calculate directions, which displays a route on the map
        directionsManager.calculateDirections();
    } else {
        //Load the Directions module and create a directions manager.
        Microsoft.Maps.loadModule('Microsoft.Maps.Directions', {
            callback: function () {
                //Create the directions manager
                directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

                //Perfrom route logic
                GetRoute();
            }
        });
    }
}

/******* Common Functions ******/

function ClearMap() {
    map.entities.clear();

    if (directionsManager) {
        directionsManager.resetDirections();
    }
}

function ShowMessage(title, msg) {
    var m = new Windows.UI.Popups.MessageDialog(title, msg);
    m.showAsync();
}

//Initialization logic for loading the map control
(function () {
    function initialize() {
        Microsoft.Maps.loadModule('Microsoft.Maps.Map', { callback: GetMap });
    }

    document.addEventListener("DOMContentLoaded", initialize, false);
})();